package com.org.web.handle.car.controller;

import com.org.platform.model.ResultModel;
import com.org.platform.utils.CommonUtil;
import com.org.web.handle.car.service.CarService;
import com.org.web.handle.vo.CarVo;
import com.org.web.support.base.BaseController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author devg
 * @Date 2020/3/2 10:14
 */
@RestController
@RequestMapping(value = {"/car"})
public class CarController extends BaseController {

  @Autowired
  private CarService carService;

  @RequestMapping(value = {"/index"})
  public ModelAndView index() {
    Map<String, Object> result = new HashMap<>();
    return modelAndView("/front/car", result);
  }

  @RequestMapping(value = {"/mycar"})
  public ModelAndView mycar() {
    List<CarVo> carVoList = carService.getCarList();
    Map<String, Object> result = new HashMap<>();
    result.put("carVoList", carVoList);
    result.put("carVoNum", carVoList.size());
    return modelAndView("/front/carList", result);
  }

  @RequestMapping(value = {"/addToCar"})
  public ResultModel addToCar(@Valid @RequestBody CarVo carVo) {
    return carService.addToCar(carVo);
  }

  @RequestMapping(value = {"/deleteCar"})
  public ResultModel deleteCar(@RequestBody Map<String, Object> param) {
    List<String> deleteIds = CommonUtil.getMapStringList(param, "deleteIds");
    if (!CommonUtil.isNullOrEmpty(deleteIds)) {
      return carService.deleteCar(deleteIds);
    }
    return ResultModel.newFail("商品ID不能为空");
  }
}
